<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>新增字典</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css" />
    <script src="../../../component/layui/layui.js"></script>
    <script src="../../../component/pear/pear.js"></script>
</head>

<body>
    <form class="layui-form" action="" lay-filter="add">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">字典编码</label>
                        <div class="layui-input-block">
                            <input type="text" name="fieldCode" lay-verify="required" autocomplete="off"
                                placeholder="请输入字典编码" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">字典名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="fieldName" lay-verify="required" autocomplete="off"
                                placeholder="请输入字典名称" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">字典描述</label>
                        <div class="layui-input-block">
                            <input type="text" name="explain" lay-verify="required" autocomplete="off"
                                placeholder="请输入字典描述" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="isActive" lay-skin="switch" lay-text="启用|禁用"
                                lay-filter="isActive" checked="checked">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</body>

</html>

<script>
    layui.use(['form', 'jquery', 'context', 'permission', 'popup', 'button'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let context = layui.context;
        let popup = layui.popup;
        let button = layui.button;

        let basePath = context.get("base-path");

        form.on('submit(save)', function (data) {
            var btn = button.load({ elem: 'button[lay-filter=save]' });
            data.field.isActive = data.field.isActive == "on" ? true : false;
            $.ajax({
                url: basePath + '/api/system/dict/addfield',
                data: JSON.stringify(data.field),
                success: function (result) {
                    btn.stop(function () {
                        if (result.succeeded) {
                            popup.success("添加成功", function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.layui.table.reload("dictFieldTable");
                            });
                        } else {
                            popup.failure(JSON.stringify(result.errors));
                        }
                    })
                }
            })
            return false;
        });
    })
</script>